﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.CMS.MODEL;

namespace Jxym.wlkc2011
{
    public partial class kcms : System.Web.UI.UserControl
    {
        #region 字段|属性

        private string _img = "";
        public string MyImg
        {
            get { return _img; }
            set { _img = value; }
        }
        private string _height = "";
        private int _l = 100;
        private string _href = "#";
        private string _title = "未命名";
        private int _myContentClassID = 1;
        private int _topN = 10;
        public string MyHeight
        {
            get { return _height; }
            set { _height = value; }
        }
        public string MyTitle
        {
            get { return _title; }
            set { _title = value; }
        }
        public string MyHref
        {
            get { return _href; }
            set { _href = value; }
        }
        public int MyContentClassID
        {
            get { return _myContentClassID; }
            set { _myContentClassID = value; }
        }
        public int TopN
        {
            get { return _topN; }
            set { _topN = value; }
        }
        public int L
        {
            get { return _l; }
            set { _l = value; }
        }
        private int _menuId = 0;

        public string _siteName { get; set; }

        public string SiteTitle { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int _height2 = Convert.ToInt32(_height);

                if (Request.Params["siteName"] != null)
                {
                    _siteName = Request.Params["siteName"];

                    //加载网站配置信息
                    MsSqlDal<Cms_Site> site = new MsSqlDal<Cms_Site>();

                    Cms_Site Info = site.GetModel("Name='" + _siteName + "'", null);

                    if (Info != null)
                    {
                        //加载网站名称
                        SiteTitle = Info.Title;

                        xxCount = GetKcxx(_siteName);
                    }

                    _height = _height != "" ? "style=\"height:" + _height + "px;overflow:hidden;\"" : _height;

                    _href = _href != "#" ? "<span class=\"more\"><a href=\"" + (_siteName + "_" + _href) + "\">&nbsp;</a></span>" : "";

                    if (_img != "")
                    {
                        Panel1.Visible = true;
                        Literal1.Text = "<img src=\"" + _img + "\" style=\"width:100%; height:" + (_height2 - 34) + "px;\" />";
                        return;
                    }
                    if (Request.Params["menuid"] != null)
                    {
                        _menuId = Convert.ToInt32(Request.Params["menuid"]);
                    }

                    GetKcms(_siteName);
                }
            }
        }

        private void GetKcms(string siteName)
        {
            MsSqlDal<Cms_ContentClass> ccc = new MsSqlDal<Cms_ContentClass>();

            IList<Cms_ContentClass> ccclist = ccc.GetList("siteName='" + _siteName + "'", null, "", 0);

            if (ccclist.Count > 0)
            {
                foreach (Cms_ContentClass cccInfo in ccclist)
                {
                    if (cccInfo.Title.Contains("课程描述"))
                    {
                        MsSqlDal<Cms_Content> cc = new MsSqlDal<Cms_Content>();

                        Cms_Content ccInfo = cc.GetModel("ContentClassID=" + cccInfo.ID, null);
                        if (ccInfo != null)
                        {
                            Panel1.Visible = true;
                            Literal1.Text = Jxym.Tools.Char.MySubstring(Jxym.Tools.Char.DelHTML(ccInfo.Html), _l);
                            Literal2.Text = "<img src=\"" + "UpFiles/" + _siteName + "/" + Jxym.Tools.Char.MySubstring(ccInfo.ViewPhoto, _l) + "\" style=\" height:207px; width:322px;border:0px;" + "\" class=\"img\" />";
                        }
                        return;
                    }
                }
            }
            Literal1.Text = "暂无信息";
        }

        private int GetKcxx(string _siteName)
        {
            MsSqlDal<Cms_Site_ContentClass> cscc = new MsSqlDal<Cms_Site_ContentClass>();

            MsSqlDal<Cms_ContentClass> ccc = new MsSqlDal<Cms_ContentClass>();

            IList<Cms_Site_ContentClass> csccList = cscc.GetList("SiteName='" + _siteName + "'", null, "", 0);

            IList<Cms_ContentClass> cccList = new List<Cms_ContentClass>();

            foreach (Cms_Site_ContentClass csccInfo in csccList)
            {
                Cms_ContentClass cccInfo = ccc.GetModel(csccInfo.ContentClassID);

                if (cccInfo != null)
                {
                    if (cccInfo.Title.Contains("课程学习"))
                    {
                        cccList = ccc.GetList("ParentID=" + cccInfo.ID, null, "", 0);
                        return cccList.Count;
                    }
                }
            }

            return 0;
        }

        public int xxCount { get; set; }
    }
}